﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BYSJ.Model;
using BYSJ.IBLL;
using BYSJ.BLL;
using BYSJ.Web.Filters;

namespace BYSJ.Web.Controllers
{
    public class LognController : Controller
    {
        IUserSetService UserServic = null;
        public LognController()
        {
            UserServic = new UserSetService();
        }
        // GET: /Logn/
     
        public ActionResult Index()
        {
            return View();
        }
        [AdminFilter]
        public ActionResult longout()
        {
            //退出登录的原理是把session变为null
            Session["userSession"] = null;
            return new RedirectResult("/Logn/Index");
        }

        public string Logn(UserSet user)
        {
            List<UserSet> dbuser = new List<UserSet>();
            dbuser=UserServic.LoadEntities(u => u.Name == user.Name && u.PassWord == user.PassWord).ToList();
            if (dbuser.Count == 1)
            {
                //如果登录成功，跳转到首页
               //return View("~/Views/Home/Index.cshtml");
                //return Redirect("/Home/Index");
                HttpCookie cookiename = new HttpCookie("cookiename");
                cookiename.Value = dbuser.First().id.ToString();
                cookiename.Path = "/";
                cookiename.Expires = DateTime.Now.AddMinutes(300);
                Response.Cookies.Add(cookiename);
                Session["userSession"] = dbuser.First().Name;
                return user.Name;
 
            }
            else
            {
                //如果不成功，在此登录
                //return View("~/Views/Logn/Index.cshtml");
                //return RedirectToAction("Index");
                return "账号异常";
            }
        }
    }
}
